package abg.core.service.os;

import abg.core.domain.os.OsTye;
import com.sun.management.OperatingSystemMXBean;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;

/**
 * OS
 */
public class OsKit {

    public static OsTye getOsType() {
        String osName = System.getProperty("os.name").toLowerCase();

        if (osName.contains("win")) return OsTye.Win;

        if (osName.contains("nix") || osName.contains("nux")) {
            String distro = getLinuxDistro();
            if (distro.equalsIgnoreCase("ubuntu")) return OsTye.Ubuntu;
            if (distro.equalsIgnoreCase("centos")) return OsTye.Centos;
            if (osName.contains("nux")) return OsTye.Linux;
            if (osName.contains("nix")) return OsTye.Unix;
        }

        if (osName.contains("mac")) return OsTye.Mac;

        return OsTye.Other;
    }

    public static String getLinuxDistro() {
        try (BufferedReader reader = new BufferedReader(new FileReader("/etc/os-release"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("ID=")) {
                    return line.substring(3).replaceAll("\"", "");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "Unknown";
    }

    public static String getCpu() {

        OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        // 获取CPU核心数量
        int availableProcessors = osBean.getAvailableProcessors();
        System.out.println("CPU核心数量: " + availableProcessors);

        // 获取系统负载平均值
        double systemLoadAverage = osBean.getSystemLoadAverage();
        System.out.println("系统负载平均值: " + systemLoadAverage);

        // 获取CPU架构
        String osArch = System.getProperty("os.arch");
        System.out.println("CPU架构: " + osArch);

        return "Unknown";
    }


    public static void main(String[] args) throws InterruptedException {

    }

}
